<template>
  <div class="rollContainer">
    <div class="searchHead" ref="searchHead">
      <Form ref="formInline" :model="conditions" inline :label-width="100" :show-message="false">
        <div class="searchBox">
          <div class="conditionBox">
            <Row>
              <Col :span="6">
                <FormItem label="昵称：">
                  <Input v-model.trim="conditions.nickname" clearable placeholder="昵称"></Input>
                </FormItem>
              </Col>
              <Col :span="6">
                <FormItem label="手机号：">
                  <Input v-model.trim="conditions.mobile" clearable placeholder="手机号"></Input>
                </FormItem>
              </Col>
              <Col :span="6">
                <FormItem label="展会名称：" prop="miceId">
                  <Select
                    v-model="conditions.miceId"
                    clearable
                    @on-change="changeStatus($event, 'miceId')"
                  >
                    <Option
                      v-for="item in mices"
                      :value="item.value"
                      :key="item.value"
                    >{{ item.label }}</Option>
                  </Select>
                </FormItem>
              </Col>
            </Row>
          </div>
          <div class="searchBtnBox">
            <Button type="primary" @click="search()">搜索</Button>
            <Button @click="handleReset()">重置</Button>
          </div>
        </div>
      </Form>
    </div>
    <Table :columns="columns1" :height="tableHeight" border :data="list" :loading="loading">
      <template slot-scope="{ row }" slot="avatar">
        <img :src="row.avatarurl" alt />
      </template>
      <template slot-scope="{ row }" slot="create_time">
        <span>{{row.create_time|dateformat('YYYY-MM-DD HH:mm:ss')}}</span>
      </template>
      <template slot-scope="{ row }" slot="gender">
        <span>{{row.gender==1?'男':'女'}}</span>
      </template>
      <template slot-scope="{ row }" slot="class">
        <span v-if="row.class=='0'">普通用户</span>
        <span v-if="row.class=='1'">展商</span>
        <span v-if="row.class=='2'">业务员</span>
      </template>
      <template slot-scope="{ row }" slot="action">
        <span class="tableBtn" @click="show(row)">
          <Icon size="18" type="ios-create-outline" />编辑
        </span>
        <!-- <span class="tableBtn" @click="remove(row.id)">
          <Icon size="18" type="ios-trash" />删除
        </span>-->
      </template>
    </Table>
    <div class="pageBox">
      <Page
        :total="total"
        :page-size="pageSize"
        :current="page"
        size="small"
        show-total
        show-sizer
        @on-change="changePage"
        @on-page-size-change="changeSize"
      />
    </div>
    <Modal v-model="isFixed" title="修改个人信息">
      <Form ref="formItem" :model="formItem" :rules="ruleInline" inline>
        <FormItem label="角色名称：" prop="class">
          <Select
            v-model="formItem.class"
            clearable
            @on-change="checkStatus($event, 'class','formItem')"
          >
            <Option v-for="item in classlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button @click="Itemcancel">取消</Button>
        <Button type="primary" @click="submit('formItem')">确定</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import { wxUserList, modifyUser } from "../../api/account";
import { miceList } from "../../api/exhibitors";
export default {
  data() {
    return {
      list: [],
      mices: [],
      page: 1,
      pageSize: 20,
      total: 0,
      loading: false,
      conditions: {
        nickname: "",
        mobile: "",
        miceId: ""
      },
      columns1: [
         {
          title: "openid",
          key: "openid",
          align: "center"
        },
        {
          title: "手机号",
          key: "mobile",
          align: "center"
        },
        {
          title: "账户名",
          key: "nickname",
          align: "center"
        },
        {
          title: "头像",
          slot: "avatar",
          align: "center"
        },
        {
          title: "性别",
          slot: "gender",
          align: "center",
          width: 100
        },
        {
          title: "用户等级",
          slot: "class",
          align: "center",
          width: 150
        },
        {
          title: "绑定展会",
          key: "miceId",
          align: "center"
        },
        {
          title: "创建时间",
          slot: "create_time",
          align: "center"
        },

        {
          title: "操作",
          slot: "action",
          width: 200,
          align: "center"
        }
      ],
      tableHeight: 100,
      isFixed: false,
      formItem: {
        class: ""
      },
      openid: "",
      ruleInline: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入账号密码",
            trigger: "blur"
          },
          {
            type: "string",
            min: 6,
            message: "密码需超过6位数",
            trigger: "blur"
          }
        ],
        realname: [
          {
            required: true,
            message: "请输入真实姓名",
            trigger: "blur"
          }
        ]
      },
      classlist: [
        {
          label: "普通用户",
          value: "0"
        },
        {
          label: "展商",
          value: "1"
        },
        {
          label: "业务员",
          value: "2"
        }
      ]
    };
  },
  created() {
    this.getmiceList(0);
    this.getuserList();
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight =
        window.innerHeight - this.$refs.searchHead.offsetHeight - 220;
        console.log(this.tableHeight)
    });
  },
  methods: {
    checkStatus(e, name, pname) {
      this[pname][name] = e;
    },
    getmiceList(type) {
      miceList({
        type
      }).then(res => {
        if (res.status) {
          this.mices = res.data.list.map(e => {
            return {
              value: e.miceId,
              label: e.miceName
            };
          });
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
    search() {
      this.getuserList();
    },
    changeStatus(e, name) {
      this.page = 1;
      this.conditions[name] = e;
      this.getuserList();
    },
    handleReset() {
      this.conditions = {
        nickname: "",
        mobile: "",
        miceId: ""
      };
      this.getuserList();
    },
    getuserList() {
      this.loading = true;
      const { page, pageSize } = this;
      const { nickname, mobile, miceId } = this.conditions;
      wxUserList({ page: page - 1, pageSize, nickname, mobile, miceId }).then(
        res => {
          if (res.status) {
            this.total = res.data.count;
            this.list = res.data.list;
            this.loading = false;
          } else {
            this.loading = false;
            this.$Message.error(res.msg);
          }
        }
      );
    },
    show(row) {
      this.formItem = {
        class: row.class
      };
      this.openid = row.openid;
      this.isFixed = true;
    },
    changePage(e) {
      this.page = e;
      this.getuserList();
    },
    changeSize(e) {
      this.page = 1;
      this.pageSize = e;
      this.getuserList();
    },
    submit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          modifyUser({
            class: this.formItem.class,
            openid: this.openid
          }).then(res => {
            if (res.status) {
              this.isFixed = false;
              this.getuserList();
              this.$Message.success(res.msg);
            } else {
              this.$Message.error(res.msg);
            }
          });
        } else {
        }
      });
    },
    Itemcancel() {
      this.isFixed = false;
    },
    cancel() {
      this.isAdd = false;
    }
  }
};
</script>
<style lang="less" scoped>
.page {
  display: flex;
  justify-content: flex-end;
  padding: 20px 0;
}
</style>
